using System;
using System.Diagnostics;

namespace AsyncLoggers
{
    /// <summary>
    /// An asynchronous <see cref="TraceListener"/>
    /// 
    /// Note: not thread-safe
    /// </summary>
    public class AsyncTraceListener : TraceListener
    {
        private readonly TraceListener _traceListener;
        private readonly CommandProcessor _commandProcessor = new CommandProcessor();

        /// <summary>
        /// Creates an asynchronous <see cref="TraceListener"/>
        /// </summary>
        /// <param name="traceListener"></param>
        public AsyncTraceListener(TraceListener traceListener)
        {
            if (traceListener == null)
                throw new ArgumentNullException("traceListener");

            if (traceListener is AsyncTraceListener)
                throw new ArgumentException("Wrapping an AsyncTraceListener into another AsyncTraceListener is prohibited", "traceListener");

            _traceListener = traceListener;
        }

        /// <summary>
        /// When overridden in a derived class, flushes the output buffer.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public override void Flush()
        {
            _commandProcessor.ProcessQueuedCommands();

            _traceListener.Flush();
        }

        /// <summary>
        /// When overridden in a derived class, closes the output stream so it no longer receives tracing or debugging output.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public override void Close()
        {
            Dispose();
        }

        /// <summary>
        /// Gets or sets a name for this <see cref="T:System.Diagnostics.TraceListener"/>.
        /// </summary>
        /// <returns>
        /// A name for this <see cref="T:System.Diagnostics.TraceListener"/>. The default is an empty string ("").
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string Name
        {
            get
            {
                return _traceListener.Name;
            }
            set
            {
                _traceListener.Name = value;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the trace listener is thread safe. 
        /// </summary>
        /// <returns>
        /// true if the trace listener is thread safe; otherwise, false. The default is false.
        /// </returns>
        public override bool IsThreadSafe
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="T:System.Diagnostics.TraceListener"/> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
        protected override void Dispose(bool disposing)
        {
            _commandProcessor.Dispose();

            _traceListener.Dispose();
        }

        /// <summary>
        /// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
        /// </summary>
        /// <param name="message">A message to write.</param>
        /// <filterpriority>2</filterpriority>
        public override void Write(string message)
        {
            _commandProcessor.QueueCommand(new ActionCommand(() => _traceListener.Write(message)));
        }
        
        /// <summary>
        /// When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
        /// </summary>
        /// <param name="message">A message to write. </param><filterpriority>2</filterpriority>
        public override void WriteLine(string message)
        {
            _commandProcessor.QueueCommand(new ActionCommand(() => _traceListener.WriteLine(message)));
        }
    }
}